package com.controller.home;

import com.controller.BaseController;
import com.dao.UserAddressMapper;
import com.entity.Goods;
import com.entity.Size;
import com.entity.UserAddress;
import com.entity.UserCartLog;
import com.service.imple.home.GoodsImple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("home/homepage")
public class GoodsController extends BaseController<GoodsImple> {

    @Autowired
    private UserAddressMapper userAddressMapper;

    //首页
    @RequestMapping("/index")
    public String index(ModelMap modelMap){
        List<Goods> list = BaseService.selectAll(null);
        modelMap.put("list",list);
        return "homepage/index";
    }

    @RequestMapping("like")
    public String Like(String name, ModelMap modelMap){
        Goods goods = new Goods();
        goods.setLikeName(name);
        List<Goods> list = BaseService.selectLike(goods);
        modelMap.put("list",list);

        return "homepage/productlist";
    }

    //商品详情
    @RequestMapping("productDetail")
    public String productDetail(ModelMap modelMap, Integer id){
        Goods goods = new Goods();
        goods.setId(id);
        List<Goods> list = BaseService.selectAll(goods);
        list.get(0).setClickCount(list.get(0).getClickCount()+1);
        BaseService.update(list.get(0));
        modelMap.put("list",list);
        return "homepage/productDetail";
    }

    //立即购买
    @RequestMapping("toWriteorder")
    public String toWriteorder(@SessionAttribute("userId")Integer userId, ModelMap modelMap, Integer id){
        Goods goods = new Goods();
        goods.setId(id);
        List<Goods> list = BaseService.selectAll(goods);



        Integer totalPrice = list.get(0).getSellPrice();
        modelMap.put("totalPrice", totalPrice);
        modelMap.put("list",list);
        System.out.println(totalPrice);
        modelMap.put("list",list);
        modelMap.put("totalPrice", totalPrice);
        return "homepage/writeorder2";
    }


    //商品列表
    @RequestMapping("/toProductList")
    public String toProductList(Integer catalogId, ModelMap modelMap){
        Goods goods = new Goods();
        goods.setCatalogId(catalogId);
        List<Goods> list = BaseService.selectAll(goods);
        modelMap.put("list",list);
        return "homepage/productlist";
    }

    @RequestMapping("/toProductListPrice")
    public String toProductListPrice(Size size, ModelMap modelMap){
        List<Goods> list = BaseService.selectPrice(size);
        modelMap.put("list",list);
        return "homepage/productlist";
    }

    @RequestMapping("/toProductListDate")
    public String toProductListDate( ModelMap modelMap){
        List<Goods> list = BaseService.selectDate(null);
        modelMap.put("list",list);
        return "homepage/productlist";
    }

    @RequestMapping("/toProductListClick")
    public String toProductListClick( ModelMap modelMap){
        List<Goods> list = BaseService.selectClick(null);
        modelMap.put("list",list);
        return "homepage/productlist";
    }

    @RequestMapping("/toProductListPriceDesc")
    public String toProductListPriceDesc( ModelMap modelMap){
        List<Goods> list = BaseService.selectPriceDesc(null);
        modelMap.put("list",list);
        return "homepage/productlist";
    }

    @RequestMapping("/toProductListPriceAsc")
    public String toProductListPriceAsc( ModelMap modelMap){
        List<Goods> list = BaseService.selectPriceAsc(null);
        modelMap.put("list",list);
        return "homepage/productlist";
    }

}
